package com.wenjiang.controller;

import com.wenjiang.common.annotation.OperatorLog;
import com.wenjiang.common.dto.PageParam;
import com.wenjiang.common.lang.Result;
import com.wenjiang.entity.MallGood;
import com.wenjiang.service.MallGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/good")
public class GoodController {

    @Autowired
    MallGoodService goodService;

    // 添加一个商品
    @RequestMapping("/add")
    @OperatorLog
    public  Object addGood(HttpServletRequest request , @RequestParam("file")MultipartFile file){
       //获取参数
        String goodName = request.getParameter("goodName");
        String goodType = request.getParameter("goodType");
        String goodInfo = request.getParameter("goodInfo");
        String goodPrice = request.getParameter("goodPrice");
        String goodNowPrice = request.getParameter("goodNowPrice");
        String goodCount = request.getParameter("goodCount");
        String goodStatus = request.getParameter("goodStatus");


        MallGood good =new MallGood();

        good.setGoodName(goodName);
        good.setGoodInfo(goodInfo);
        good.setGoodType(Integer.parseInt(goodType));
        good.setGoodPrice(Double.valueOf(goodPrice));
        good.setGoodNowPrice(Double.valueOf(goodNowPrice));
        good.setGoodCount(Integer.parseInt(goodCount));
        good.setGoodStatus(Integer.parseInt(goodStatus));

        Result result = goodService.addGood(good, file);
        return  result;
    }

    // 分页获取商品

    @RequestMapping("/page")
    public  Object getPageGood(@RequestBody PageParam pageParam){

        return goodService.getPageGood(pageParam);
    }

    // 获取商品总数
    @RequestMapping("/count")
    public Object getGoodCount(){

     return   goodService.getGoodCount();
    }


    //编辑一个商品
    @RequestMapping("/update")
    @OperatorLog
    public  Object updateGood(@RequestBody MallGood mallGood){

        return  goodService.setGood(mallGood);
    }


    //删除一个商品
    @RequestMapping("/delete/{goodId}")
    @OperatorLog
    public  Object delGood(@PathVariable("goodId") Long goodId){
        return  goodService.delGood(goodId);
    }

}
